<template>
  <card-wrapper :data-item="dataItem" class="w-256">
    <area-top :text-left="dataItem['领料单号'] || '无'" :text-right="`${dataItem['数量'] || '无'} ${dataItem['物料单位'] || '无'}`" />
    <!-- <template #header>
      <span class="title">原材料</span>
    </template> -->
    <div class="mt-4">
      <div v-for="(item, index) in list" :key="index" class="py-8 border-white border-solid text-14 color-333333 leading-14 border-b-1">
        <div v-if="index === 0" class="flex justify-between">
          <div>
            <p>{{ item['物料名称'] || '无' }}</p>
            <p>
              (<span class="color-999999">{{ item['物料编码'] || '无' }}</span
              >)
            </p>
          </div>
          <div class="list-num" v-if="pageNum === 1 && !loadAll" @click="unfold">+{{ dataItem.searchResults.length }}</div>
        </div>
        <div class="flex justify-between" v-else>
          <div>
            <span>{{ item['物料名称'] || '无' }}</span>
            (<span class="color-999999">{{ item['物料编码'] || '无' }}</span
            >)
          </div>
          <div class="flex align-center" v-if="index > 0 && index === list.length - 1 && dataItem.searchResults.length > pageSize">
            <div class="list-num" v-if="!loadAll" @click="unfold">+{{ dataItem.searchResults.length }}</div>
            <svg-icon icon-class="icon_fold_second" class="ml-2" @click="packUp" />
          </div>
        </div>
      </div>
      <div class="load-all" v-if="loadAll">{{ $i18n.t("mind.allDataHasBeenExpanded") }}</div>
    </div>
    <LabelNormal :value="dataItem['工单'] || '无'" :type="$i18n.t('mind.workOrder')" className="color-418CBD" class="mt-8" />
    <label-normal :value="`${dataItem['操作人'] || '无'}  ${dataItem['操作时间'] || '无'}`" class="mt-8" />
  </card-wrapper>
</template>

<script>
import CardWrapper from '@/views/mind/components/common/card-wrapper.vue'
import AreaTop from '@/views/mind/components/common/area-top.vue'
import LabelNormal from '@/views/mind/components/common/label-normal.vue'
export default {
  name: 'card-picking', // 领料
  components: {
    CardWrapper,
    AreaTop,
    LabelNormal
  },
  props: {
    dataItem: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      pageNum: 1,
      pageSize: 5,
      list: [],
      loadAll: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      const { searchResults } = this.dataItem
      if (searchResults.length > 0 && searchResults.length > this.pageSize) {
        const start = (this.pageNum - 1) * this.pageSize
        const end = start + this.pageSize
        this.list = this.list.concat(searchResults.slice(start, end))
        if (this.list.length === searchResults.length) {
          this.loadAll = true
        }
      } else {
        this.pageNum = 1
        this.list = searchResults
        this.loadAll = true
      }
    },
    unfold() {
      const { searchResults } = this.dataItem
      if (this.list.length === searchResults.length) {
        return
      }
      this.pageNum++
      this.getList()
    },
    packUp() {
      this.pageNum = 1
      this.list = []
      this.loadAll = false
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  font-size: 12px;
  padding: 2px 4px;
  border-radius: 2px 2px 0 0;
  color: #fff;
  background-color: #c682ee;
  margin-left: 4px;
}

.list-num {
  padding: 0 4px;
  height: 20px;
  border: 1px solid #9badbc;
  border-radius: 10px;
  text-align: center;
  font-size: 12;
  color: #5a6f82;
  line-height: 20px;
  &:hover {
    color: #3d82ea;
    border-color: #3d82ea;
  }
}

.load-all {
  font-size: 12px;
  color: #999;
  text-align: center;
  margin-top: 4px;
}
</style>
